package com.example.machine.service;

import com.example.machine.enums.MachineStatusEnum;
import com.example.machine.mapper.MachineInfoMapper;
import com.example.machine.model.MachineInfo;
import com.example.machine.model.PageRequest;
import com.example.machine.model.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Home-pc
 * Date: 2024-01-17
 * Time: 19:56
 */
@Slf4j
@Service
public class MachineService {

    @Autowired
    private MachineInfoMapper machineInfoMapper;

    public PageResult<MachineInfo> selectMachineInfoByPage(PageRequest pageRequest){
        if(pageRequest == null){
            return null;
        }
        //获取总记录数
        Integer count = machineInfoMapper.count();
        //获取当前记录
        List<MachineInfo> machineInfos = machineInfoMapper.selectMachineInfoByPage(pageRequest.getOffset(), pageRequest.getPageSize());

        //根据当前记录转换状态的表示形式
        if(machineInfos != null && machineInfos.size() > 0){
            for (MachineInfo machineinfo:
                    machineInfos) {
                machineinfo.setStatusCN(MachineStatusEnum.getNameByCode(machineinfo.getStatus()).getName());
            }
        }

        return new PageResult<>(machineInfos,count,pageRequest);
    }

    public Integer addMachine(MachineInfo machineInfo){
        Integer result = 0;
        try{
            result = machineInfoMapper.insertMachine(machineInfo);
        } catch (Exception e) {
            log.error("添加机台出错，e:{}",e);
        }

        return result;
    }

    public MachineInfo queryMachineInfoById(Integer id){
        return machineInfoMapper.queryMachineInfoById(id);
    }

    public Integer updateMachine(MachineInfo machineInfo){
        Integer result = 0;
        try{
            result = machineInfoMapper.updateMachine(machineInfo);
        } catch (Exception e) {
            log.error("更新图书失败",e);
        }
        return result;
    }

    public Integer batchDelete(List<Integer> ids){
        Integer result = 0;
        try{
            result = machineInfoMapper.batchDelete(ids);
        } catch (Exception e) {
            log.error("批量删除机台失败",ids);
        }
        return result;
    }

















}

